package com.example.javademo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.javademo.ui.main.FrigmentFragment;

public class Frigment extends AppCompatActivity {

    /*
    * savedInstanceState 用于保存活动的状态信息
    * */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frigment);
        /*
        * 检查savedInstanceState是否为null
        * 如果为null，说明这是活动第一次被创建
        * getSupportFragmentManager()获取FragmentManager实例
        * beginTransaction()开始一个事务
        * replace(R.id.container, FrigmentFragment.newInstance())替换容器中的Fragment
        * commitNow()立即提交事务
        * */
        if (savedInstanceState == null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.container, FrigmentFragment.newInstance());
            transaction.commitNow();
        }
    }

    public void buttonF1Click(View view) {
//        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if(view.getId() == R.id.buttonF1){
            Bundle bundle = new Bundle();
            bundle.putString("key_from_f1", "这是从Fragment1传递的数据");
            bundle.putInt("number", 123);

            replaceFragment(new FrigmentFragment(), bundle);
            /*
            * 1.实例化Fragment
            * 2.实例化Bundle
            * 3.将数据放入Bundle
            * 4.将Bundle放入Fragment
            * 5.添加或者替换显示Fragment
            * */
//            FrigmentFragment fragment = new FrigmentFragment();
//            Bundle bundle = new Bundle();
//            bundle.putString("key", "value");
//            fragment.setArguments(bundle);
//            transaction.replace(R.id.container, fragment);
        }

        if(view.getId() == R.id.buttonF2){
            Bundle bundle = new Bundle();
            bundle.putString("key_from_f2", "这是从Fragment2传递的数据");
            replaceFragment(new BlankFragment1(), bundle);
        }

        if(view.getId() == R.id.buttonF3){
            replaceFragment(new ItemFragment(),null);
        }
    }

    /*
    * 动态切换
    * */

    public void replaceFragment(androidx.fragment.app.Fragment fragment,Bundle bundle) {
        if (bundle != null) {
            fragment.setArguments(bundle);
        }

        // 获取FragmentManager
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启FragmentTransaction
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.replace(R.id.container, fragment);
//        addToBackStack 添加回退栈
//        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    public String toastMsg(){
        return "我这是主应用的消息！";
    }
}